#!/usr/bin/env sh
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

if [ -z "$ARTEMIS_INSTANCE" ] ; then

  ## resolve links - $0 may be a link to ActiveMQ's home
  PRG="$0"
  progname=`basename "$0"`
  saveddir=`pwd`

  # need this for relative symlinks
  dirname_prg=`dirname "$PRG"`
  cd "$dirname_prg"

  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '.*/.*' > /dev/null; then
    PRG="$link"
    else
    PRG=`dirname "$PRG"`"/$link"
    fi
  done

  ARTEMIS_INSTANCE=`dirname "$PRG"`
  cd "$saveddir"

  # make it fully qualified
  ARTEMIS_INSTANCE=`cd "$ARTEMIS_INSTANCE/.." && pwd`
fi

HAWTIO_ROLE="NO_HAWTIO_ROLE"

# Load Profile Data
ARTEMIS_INSTANCE_ETC='/var/lib/artemis-instance/etc'
. "$ARTEMIS_INSTANCE_ETC/artemis.profile"


CLASSPATH="$ARTEMIS_HOME/lib/artemis-boot.jar"

# OS specific support.
cygwin=false;
darwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true
    OSTYPE=cygwin
    export OSTYPE
    ;;
  Darwin*) darwin=true
    if [ -z "$JAVA_HOME" ] ; then
     JAVA_HOME=$(/usr/libexec/java_home)
    fi
    ;;
esac

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$ARTEMIS_INSTANCE" ] &&
    ARTEMIS_INSTANCE=`cygpath --unix "$ARTEMIS_INSTANCE"`
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD=java
  fi
fi

if $cygwin ; then
  JAVA_HOME=`cygpath --windows "$JAVA_HOME"`
  ARTEMIS_HOME=`cygpath --windows "$ARTEMIS_HOME"`
  CLASSPATH=`cygpath --windows "$CLASSPATH"`
fi

if [ -f "$ARTEMIS_OOME_DUMP" ] ; then
  # backup the last OOME heap dump
  mv $ARTEMIS_OOME_DUMP $ARTEMIS_OOME_DUMP.bkp
fi

exec "$JAVACMD" \
    $JAVA_ARGS \
    -Dhawtio.role="$HAWTIO_ROLE" \
    -Djava.security.auth.login.config="$ARTEMIS_INSTANCE_ETC/login.config" \
    $ARTEMIS_CLUSTER_PROPS \
    -classpath "$CLASSPATH" \
    -Dartemis.home="$ARTEMIS_HOME" \
    -Dartemis.instance="$ARTEMIS_INSTANCE" \
    -Djava.library.path="$ARTEMIS_HOME/bin/lib/linux-$(uname -m)" \
    -Djava.io.tmpdir="$ARTEMIS_INSTANCE/tmp" \
    -Ddata.dir="$ARTEMIS_DATA_DIR" \
    -Dartemis.instance.etc="$ARTEMIS_INSTANCE_ETC" \
    $DEBUG_ARGS \
    $JAVA_ARGS_APPEND \
    org.apache.activemq.artemis.boot.Artemis "$@"
